package com.example.b_308.myapplication.model.base;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tongkang on 2017/10/31.
 * 模块基类
 */

public abstract class BaseModel<T extends BaseModel> implements Serializable {

    /**
     * 属性列表
     */
    protected Map attrs;

    public BaseModel() {
        attrs = new HashMap();
    }

    public BaseModel(String json) {
        attrs = JSON.parseObject(json, Map.class);
    }

    public BaseModel(BaseModel baseModel) {
        putAttrs(baseModel);
    }

    /**
     * get方式获取数据

     */
    public void getRequestHttp(String urlk) {
    }

    /**
     * post方式提交数据
     *
     * @param url    网络地址
     */
    public void postData(String url, T t) {
    }


    /**
     * 获取属性
     *
     * @param key
     * @return
     */
    public Object get(String key) {
        return attrs.get(key);
    }

    public void put(String key, Object value) {
        attrs.put(key, value);
    }

    public void putAttrs(BaseModel baseModel) {
        if (null != baseModel)
            attrs.putAll(baseModel.getAttrs());
    }

    public String getStr(String key) {
        Object value = attrs.get(key);
        if (null == value)
            return null;
        if (value instanceof String || value instanceof JSONObject)
            return value.toString();
        if (value instanceof JSONArray)
            return ((JSONArray) value).toJSONString();
        return null;
    }

    public String getStr(String key, String defaultValue) {
        Object value = attrs.get(key);
        if (null == value)
            return null;
        if (value instanceof String || value instanceof JSONObject)
            return value.toString();
        if (value instanceof JSONArray)
            return ((JSONArray) value).toJSONString();
        return defaultValue;
    }

    public int getInt(String status) {
        Object value = attrs.get("status");
        if (null == value || !(value instanceof Integer))
            return -1;
        return (int) value;
    }

    public int getInt(String status, int defaultValue) {
        Object value = attrs.get("status");
        if (null == value || !(value instanceof Integer))
            return defaultValue;
        return (int) value;
    }

    public Map getAttrs() {
        return attrs;
    }


}
